package org.breathe.dynamic.thread.pool.core.parser.impl;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.breathe.dynamic.thread.pool.core.parser.AbstractConfigParser;
import org.breathe.thread.pool.common.enums.ConfigFileTypeEnum;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ByteArrayResource;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author: breathe
 * @createTime: 2025-04-14
 */
public class YamlConfigParser extends AbstractConfigParser {

    private static final List<ConfigFileTypeEnum> CONFIG_TYPE = Lists.newArrayList(ConfigFileTypeEnum.YAML, ConfigFileTypeEnum.YML);
    @Override
    public List<ConfigFileTypeEnum> type() {
        return CONFIG_TYPE;
    }

    @Override
    public Map<Object, Object> doParse(String content) throws IOException {
        if (StrUtil.isEmpty(content)) {
            return Maps.newHashMapWithExpectedSize(0);
        }
        YamlPropertiesFactoryBean bean = new YamlPropertiesFactoryBean();
        bean.setResources(new ByteArrayResource(content.getBytes()));
        return bean.getObject();
    }
}
